<!--#include file="p1.asp" -->
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="dist/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="dist/layuiadmin/style/admin.css" media="all">
<style>
	.layui-input {height:24px}
	.layui-container {
		width: 100%;
	}
	.layui-btn {height:32px;padding: 0 12px;line-height: 32px; }	
</style>  
</head>
<body>
<%
dim db_path
db_path= Easp.Fso.MapPath("./")
' echo db_path
if request("key") = "db" then
	session("dbName") = request("dbName")
	session("dbstr") = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Password=;Data Source="&db_path&"\dbs\" & request("dbName") & ".mdb"
	session("dbtype") = "access"
end if
conn()
'==================================================================主界面
sub main(str)
	on error resume next
%>
	<script language=javascript>
	ie = (document.all)? true:false
	if (ie){
	function ctlent(eventobject){if(event.ctrlKey && 
	window.event.keyCode==13){this.document.exesql.submit();}}
	}
	</script>
	<script language="javascript">
	function table_delete()
	{
		if (confirm("确认删除该记录吗？   该操作将不可撤销！！！"))
			return true;
		else
			return false;
	}
	</script>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">  
	<form action="?key=sql" method=post name="exesql" id="exesql">        
		<font color=red>执行sql语句：</font><font color=#999999>(每句语句以“;”结束，支持(--)SQL注释，Ctrl + Enter 快速提交)</font>&nbsp; <input type="button" class="layui-btn" value="刷新本页" onClick="javascript:location.reload()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<span onClick="document.exesql.sql.rows+=5;" style="cursor:pointer;">+</span>
		<span onClick="if(document.exesql.sql.rows>9)document.exesql.sql.rows-=5" style="cursor:pointer;">-</span>
		<div style="float:left;width:600px;">
		<textarea id="sql" name="sql" style="width:600px;" rows="9" ondblClick="this.select();" onKeyDown="ctlent()"><%=request("sql")%></textarea><br />
		<input type="checkbox" name="SchemaTable" value="1" style="border:0px;">adSchemaTables 
        <input type="checkbox" name="SchemaColumn" value="2" style="border:0px;">adSchemaColumns
        <input type="checkbox" name="SchemaProvider" value="3" style="border:0px;">adSchemaProviderTypes &nbsp; 
		分页大小：
		<select name="pageSize">
		  <%
		  if request("pageSize") <> "" and  isNumeric(request("pageSize")) then
		     echo "<option value='"&request("pageSize")&"' selected>"&request("pageSize")&"</option>"
		  else
		     echo "<option value='50'>50</option>"
		  end if
		  %>
		  <option value="10">10</option>
		  <option value="20">20</option>
		  <option value="30">30</option>
		  <option value="40">40</option>
		  <option value="50">50</option>
		  <option value="60">60</option>
		  <option value="70">70</option>
		  <option value="80">80</option>
		  <option value="90">90</option>
		  <option value="100">100</option>
		</select>
		</div>
		<div style="float:left;width:50px;padding:60px 0px 0px 5px;">
		<input type="submit" name="Submit_confirm" value="提交" class="layui-btn"> <br /> <br />  
		<input type="button" name="Submit3" value="清空" onClick="sql.value=''" class="layui-btn"><br /><br /> 
		<input type="button" name="ok" value="返回" onClick="javascript:history.go(-1)" class="layui-btn">
		</div>
	</form>  
	<div style="clear:both"></div>
	
<div class="layui-upload-drag" id="test10">
  <i class="layui-icon"></i>
  <p>点击上传，或将文件拖拽到此处</p>
</div>	
	
	<% if str = "" then %>
	<form action="?key=addtable" method="post">        
		<font color=red>创建新表：</font><br>
			<label class="layui-form-label">表&nbsp;&nbsp;名：</label>
			<div class="layui-input-inline">
				<input type="text" name="table_name" size="20" class="layui-input">
			</div><br><br>
			<label class="layui-form-label">字段数：</label>			  
			<div class="layui-input-inline"><input type="text" name="field_num" size="20" class="layui-input"></div>
		<br><br>
		<div class="layui-form-item">		
			<input type="submit" name="Submit_create" value="提交" class="layui-btn">
			<input type="reset" name="Submit32" value="重置" class="layui-btn">
		</div>
	</form> 
	<br><br>
	<%
	end if
	%>
  </div>
</div>
</div>	
<%
end sub

'==================================================================查看表结构函数
sub view(table_name)
	'table_name = 表名称
	dim rs,sql,table,primary,primarykey,i,editstr,typs
	on error resume next
	table = table_name
	Set primary = Conn.OpenSchema(adSchemaPrimaryKeys,Array(empty, empty, table))
	if primary("COLUMN_NAME") <> "" then
		primarykey = primary("COLUMN_NAME")
	end if
	primary.Close
	Set primary = Nothing
	%>
	<script language="javascript">
		function table_delete()
		{
		if (confirm("确认删除该记录吗？   该操作将不可撤销！！！"))
			return true;
		else
			return false;
		}
		
		function updateTableName(){
		if (confirm("确认修改表名吗？   该操作将不可撤销！！！"))
			{
			var name=document.getElementById("newtablename").value;
			location.href='?key=reobj&obj=table&tablename=<%=table_name%>&newtablename='+name;
			return true;
			}
		else
			return false;			
		}
		function updateFieldName(){
		if (confirm("确认修改字段名吗？"))
			{
			var fieldsname=document.getElementById("fieldsname").value;
			var newfieldsname=document.getElementById("newfieldsname").value;
			//alert(fieldsname);
			location.href='?key=reobj&obj=field&tablename=<%=table_name%>&fieldsname='+fieldsname+'&newfieldsname='+newfieldsname;
			return true;
			}
		else
			return false;			
		}	
		function setprimarykey(){
		if (confirm("确认设为主键吗？"))
			{
			var keyname=document.getElementById("keyname").value;
			location.href='?key=sql&sql=ALTER TABLE [<%=table_name%>] ADD PRIMARY KEY (['+keyname+'])';
			return true;
			}
		else
			return false;			
		}		
	</script>
	<font color=red>表：<%=table_name%></font>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="刷新本页" onClick="javascript:location.reload()" class="layui-btn"><br><br>
	<% if request("key") = "editfidlevi" then call editfidlevi() %>
	<table width="600" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC" class="layui-table" >
    <tr> 
      <td width="125" height="20" align="center">字 段 名</td>
      <td width="110" align="center">类 型</td>
      <td width="83" align="center"> 设定大小</td>
      <td width="48" align="center">允许空</td>
      <td width="76" align="center">自动编号</td>
      <td width="54" align="center">主键</td>
      <td width="82" align="center">执行操作</td>
    </tr>
    <%
	sql = "SELECT * FROM [" & table_name & "] "
	Set rs = Conn.Execute(sql)
	if err = 0 then
		For i = 0 to rs.fields.count-1-rs.fields.count
		%>
		<tr> 
		  <td height="20" align="left"><%=rs(i).name%></td>
		  <td align="left"><%=typ(rs(i).type)%></td>
		  <td align="center"><%=rs(i).definedsize%></td>
		  <td align="center"><%=iif((rs(i).Attributes and adFldIsNullable)=0,"No","Yes")%></td>
		  <td align="center"><%=iif(rs(i).Properties("ISAUTOINCREMENT") = True,"Yes","No")%></td>
		  <td align="center"><%=iif(rs(i).name = primarykey,"Yes","No")%></td>
		  <td align="center">
			<a href="?key=editfidlevi&fidle=<%=rs(i).name%>&table_name=<%=table_name%>&fidletype=<%=typ(rs(i).type)%>">修改</a>&nbsp;
			<a href="?key=sql&sql=alter table [<%=table_name%>] drop [<%=rs(i).name%>];" onClick="return table_delete();">删除</a>
		  </td>
		</tr>
		<%
			editstr = editstr&"<option value='"&rs(i).name&"'>"&rs(i).name&"</option>"
		next
		%>
		</table>
		<br>
		<a href="?key=sql&sql=select * from <%=table_name%>&table_name=<%=table_name%>&primarykey=<%=primarykey%>" class="layui-btn">浏览表记录</a> &nbsp;
		
				<a href="?key=tosql&strt=0&table_name=<%=table_name%>" class="layui-btn">导出表结构</a> &nbsp;

		
		<a href="?key=sql&sql=DROP TABLE <%=table_name%>" onClick="return table_delete();" class="layui-btn">删除表</a> &nbsp;&nbsp;&nbsp; 
			<div class="layui-input-inline">
				<input type="text" id="newtablename" name="newtablename" size="20" value="<%=table_name%>" class="layui-input">
			</div>
		<input type="button" value="修改表名" onClick="updateTableName();" class="layui-btn">
		<br><br>
<form class="layui-form" action="">	
<%
	'判断是否有主键
	if primarykey = "" then
		echo "<font color=red>该表没有主键，执行操作可能会导致数据损坏或丢失。</font><br>"
		echo "你可以将："
%>
<div class="layui-input-inline">
<%
		echo "<select name='keyname' id='keyname'>"
		For i=0 to rs.fields.count-1
			echo "<option value=" & rs(i).name & ">" & rs(i).name & "</option>"
		next
		echo "</select>&nbsp;"
%>
</div>
<div class="layui-input-inline">
<%
		echo "<input type=button value=设为主键 onclick='setprimarykey();' class=""layui-btn"">"
%>
</div>
<%
		echo "<br><br>"
end if
%>

<div class="layui-input-inline">
<%
	'显示修改字段名
	echo "<select name='fieldsname' id='fieldsname'>"
	echo "<option value=''>选择字段</option>"
	echo editstr
	echo "</select> "
%>
</div>		
		<%
		echo "改名为 "  & chr(10)
		%>
<div class="layui-input-inline">		
		<%
		echo "<input type='text' name='newfieldsname' id='newfieldsname' size='20' class=""layui-input""> "  & chr(10)
		%>
</div>
		<%
		echo "<input type=button value=修改字段名 onclick='updateFieldName();' class=""layui-btn"">"
		echo "<br><br>"
	end if
	rs.close
	set rs = nothing
%>
</form>
	<font color=red>增加字段：</font><br><br>
	<form action="?key=addfield" method="post" class="layui-form" >
	  <table width="600" height="39" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" class="layui-table">
		<tr> 
		  <td width="60" height="20" align="center">字段名</td>
		  <td width="50" height="20" align="center">类型</td>
		  <td width="58" height="20" align="center">设定大小</td>
		  <td width="64" height="20" align="center">允许空值</td>
		  <td width="66" height="20" align="center"> 自动编号</td>
		  <td width="60" height="20" align="center">可填值</td>
		  <td width="96" height="20" align="center">&nbsp;&nbsp;</td>
		</tr>
		<tr> 
		  <td width="600" height="20" align="center"> 
			<div class="layui-input-inline">
				<input type="text" name="fldname" autocomplete="off" class="layui-input">
			</div>
		  </td>
		  <td width="190" height="20" align="center"> 
			<% fieldtypelist(0) %>
		  </td>
		  <td width="158" height="20" align="center"> 
			<div class="layui-input-inline">
				<input type="text" name="fldsize" autocomplete="off" class="layui-input">
			</div>
		  </td>
		  <td width="194" height="20" align="center"> 
			<input name="null" type="checkbox" value="ON" checked lay-skin="primary" >
		  </td>
		  <td width="66" height="20" align="center"> 
			<input type="checkbox" name="autoincrement" value="ON" lay-skin="primary" >
		  </td>
		  <td width="260" height="20" align="center"> 
			<div class="layui-input-inline">
				<input type="text" name="canValue" autocomplete="off" class="layui-input">
			</div>			
		  </td>		  
		  <td width="96" height="20" align="center"> 
			<input type="hidden" name="table_name" value="<%=table_name%>">
			<input type="submit" value="提交" class="layui-btn">
		  </td>
		</tr>
	</table>
	</form>
	<%
end sub

'==================================================================构建创建表的SQL语句
sub create_a_table(table_name)
	dim sql,i,primarykey,primary,rs
	on error resume next

	sql = "SELECT * FROM [" & table_name & "] "
	Set rs = Conn.Execute(sql)
	if err <> 0 then
		echo err.description
	end if
	Set primary = Conn.OpenSchema(adSchemaPrimaryKeys,Array(empty, empty, table_name))
	if primary("COLUMN_NAME") <> "" then
		primarykey = primary("COLUMN_NAME")
	end if
	primary.Close
	Set primary = Nothing

	sql = "CREATE TABLE ["&table_name & "_A" &"] ("
	For i = 0 to rs.fields.count-1
	   sql = sql & "[" & rs(i).name & "] " & typ(rs(i).type)
		  if rs(i).definedsize <> "" and instr(typ(rs(i).type),"Int")=0 then
			  sql = sql & "(" & rs(i).definedsize & ")"
		  end if
		  if (rs(i).Attributes and adFldIsNullable)=0 then
			  sql = sql & " not null"
		  end if
		  if rs(i).Properties("ISAUTOINCREMENT") = True then
			  sql = sql & " identity"
		  end if
		  sql = sql & ","
		  
		 ' if primarykey <> "" then
		    ' sql = sql & ","
		 ' end if
	next
	if primarykey<>"" then
	   sql=sql&" primary key (["&primarykey&"]) "
	else
		sql=left(sql,len(sql)-1)
	end if
	sql = sql & ")"
	sql = replace(sql,"()","")  '构建空表
	' echo sql
	response.redirect "?key=sql&sql=" & sql 
end sub

'==================================================================修改字段属性的界面
sub editfidlevi()
	dim sql,rs,i
	on error resume next
	sql = "Select * From [" & request("table_name") & "]"
	set rs = conn.execute(sql)
	for i = 0 to rs.fields.count - 1
		if rs(i).name = request("fidle") then
		%>
		<script LANGUAGE="JavaScript">
			function validate(theForm) {
				if (theForm.type.value == "")
				{
				alert("请输入数据类型");
				theForm.type.focus();
				return (false);
				}
				return (true);
		    }
		</script>
		<font color=red>修改字段属性：</font>
		<form action="?key=editfidle&fidle=<%=request("fidle")%>&table_name=<%=request("table_name")%>" method="post" name=frm onSubmit="return validate(frm)">
		<table width="600" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" class="layui-table">
		  <tr> 
			<td width="60" height="20" align="center">字段名</td>
			<td width="50" height="20" align="center">类型</td>
			<td width="58" height="20" align="center">设定大小</td>
			<td width="64" height="20" align="center">允许空值</td>
			<td width="66" height="20" align="center">自动编号</td>
  		    <td width="60" height="20" align="center">可填值</td>
			<td width="96" height="20"></td>
		  </tr>
		  <tr> 
			<td width="60" height="20" align="center"><%=rs(i).name%></td>
			<td width="50" height="20" align="center"> 
			<% fieldtypelist(request("fidletype")) %>
			  </td>
			  <td width="58" height="20"><input type="text" name="size" size="10"></td>
			  
			  <td width="64" height="20" align="center">
			  <input type="checkbox" name="null" value="null"<%=iif((rs(i).Attributes and adFldIsNullable)=0,""," checked")%>>
			  </td>
			  <td width="66" height="20" align="center"> 
			  <input type="checkbox" name="autoincrement" value="y"<%=iif(rs(i).Properties("ISAUTOINCREMENT") = True," checked","")%>>
			  </td>
		  <td width="60" height="20" align="center"> 
			<input type="text" name="canValue" size="10">
		  </td>				  
			  <td width="96" height="20" align="center"> 
			  <input type="submit" name="Submit" value="提交" class="layui-btn">
			  </td>
			</tr>
		  </table><br>
		</form>
		<%
		end if
	next
end sub

'==================================================================分页导航
'分页导航
sub showNavBar (rs,page,pageUrl,pageSize)
	page = cint(page)
	%>
	<table width="100%" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
	<tr>
	  <% if request("primarykey") <> "" and request("table_name") <> "" then %>
	  <td align="left">当前表：<font color=red><%=request("table_name")%></font>&nbsp;&nbsp;&nbsp;&nbsp;
	  <a href="?key=edit&table_name=<%=request("table_name")%>&primarykey=<%=request("primarykey")%>&action=add">插入新记录</a> 
	  </td>
	  <% end if %>
	  <td align="right">
		<%
		' echo "共有" & rs.recordCount & "条记录 当前" & page & "/" & rs.PageCount & "页"
	    if page > 1 then
			echo "<a href='" & pageUrl & "&page=1&pageSize="&pageSize&"'>首页</a> " 
			echo "<a href='" & pageUrl & "&page=" & page - 1 & "&pageSize="&pageSize&"'>上页</a> "
	    end if
		if (rs.PageCount > 1 and page < rs.PageCount) then
			echo "<a href='" & pageUrl & "&page=" & page + 1 & "&pageSize="&pageSize&"'>下页</a> "
			echo "<a href='" & pageUrl & "&page=" & rs.pageCount & "&pageSize="&pageSize&"'>末页</a> "
		end if
		' echo "转到:第"
		echo "<select name='select2' onChange='location.href=this.value;'>"
		dim i
		for i = 1 to rs.PageCount
			echo "<option value='"& pageUrl &"&pageSize="&pageSize&"&page="& i & "' "
			if i = cint(page) then echo "selected"
			echo ">"& i &"</option>"
		next
		echo "</select>Page"
	    %>
		</td>
	</tr>
	</table>
	<%
end sub
%>
<div class="layui-container">
<div class="layui-col-md3">
<table width="600" border="0" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC" class="layui-table">
		<tr><td>当前库：<font color=red><%=session("dbName")%></font></td></tr>
		<tr><td><a href="?key=db&dbName=<%=session("dbName")%>">创建新表</a></td></tr>
<%
dim objSchema
set objSchema = Conn.OpenSchema(adSchemaTables)
Do While Not objSchema.EOF
if objSchema("TABLE_TYPE") = "TABLE" then
%>
		<tr> <td>
<%
	echo "<a href='?key=view&table_name="& objSchema("TABLE_NAME") &"'>" & objSchema("TABLE_NAME") & "</a><br>"
%>
		</td></tr>
<%
end if
objSchema.MoveNext
Loop
%>
</table>
</div>
<div class="layui-col-md9">
<%
select case request("key")
case "db" '显示主界面
  call main("")
case "addtable" '显示创建表界面
  call add_table(request("table_name"),request("field_num"))
case "createtable" '执行创建表
  call create_table()
case "view"
  call view(request("table_name"))
case "sql"
  call main("1")
  call exesql(trim(request("sql")))
case "addfield"
  call addfield()
case "editfidlevi"
  call view(request("table_name"))
case "editfidle"
  call editfidle()
case "exit"
  session("dbtype") = ""
  session("dbstr") = ""
  response.redirect "?"
case "edit"
  call EditData()
case "reobj"
  call reobj()
end select
%>
</div>
</div>
  <script src="dist/layuiadmin/layui/layui.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>  
  <script>
	  
  
  layui.config({
    base: 'dist/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'contlist', 'table','form','upload','layer'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,form = layui.form,upload = layui.upload,layer=layui.layer;
	form.render(); //更新全部
	form.render('select'); //刷新select选择框渲染
	
form.verify({
  username: function(value, item){ //value：表单的值、item：表单的DOM对象
    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
      return '用户名不能有特殊字符';
    }
    if(/(^\_)|(\__)|(\_+$)/.test(value)){
      return '用户名首尾不能出现下划线\'_\'';
    }
    if(/^\d+\d+\d$/.test(value)){
      return '用户名不能全为数字';
    }
  }
  
  //我们既支持上述函数式的方式，也支持下述数组的形式
  //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
  ,pass: [
    /^[\S]{6,12}$/
    ,'密码必须6到12位，且不能出现空格'
  ] 
}); 
	
	//document.getElementById('exesql').style.display='none';
	
  //拖拽上传
  upload.render({
    elem: '#test10'
    ,url: 'upload.asp?dbName=<%=easp.var("dbName")%>'
    ,accept: 'file' //普通文件
    ,exts: 'zip|rar|7z|pdf|qes' //允许上传
    ,done: function(res){
      console.log(res);
	  layer.msg('上传成功！！'); 
    }
  });	
    var table = layui.table;
    var active = {
      addtable:function(){
	    alert("abc");
		var table_name=$("#table_name").val();
		location.href = '?key=addtable&field_num=1&table_name='+table_name;
	  }
    }  
    $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  
  });
  </script>
</body>
</html>